package com.ruoyi.hx.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.UUID;

import com.ruoyi.hx.domain.Order;
import com.ruoyi.hx.mapper.OrderMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.hx.mapper.OrderDetailMapper;
import com.ruoyi.hx.domain.OrderDetail;
import com.ruoyi.hx.service.IOrderDetailService;
import com.ruoyi.common.core.text.Convert;

/**
 * 订单详情Service业务层处理
 * 
 * @author whx
 * @date 2020-11-20
 */
@Service
public class OrderDetailServiceImpl implements IOrderDetailService 
{
    @Autowired
    private OrderDetailMapper orderDetailMapper;

    @Autowired
    private OrderMapper orderMapper;

    /**
     * 查询订单详情
     * 
     * @param goodsId 订单详情ID
     * @return 订单详情
     */
    @Override
    public OrderDetail selectOrderDetailById(Long goodsId)
    {
        return orderDetailMapper.selectOrderDetailById(goodsId);
    }

    /**
     * 查询订单详情列表
     * 
     * @param orderDetail 订单详情
     * @return 订单详情
     */
    @Override
    public List<OrderDetail> selectOrderDetailList(OrderDetail orderDetail)
    {
        return orderDetailMapper.selectOrderDetailList(orderDetail);
    }

    /**
     * 新增订单详情
     * 
     * @param orderDetail 订单详情
     * @return 结果
     */
    @Override
    public int insertOrderDetail(OrderDetail orderDetail)
    {
        return orderDetailMapper.insertOrderDetail(orderDetail);
    }

    /**
     * 修改订单详情
     * 
     * @param orderDetail 订单详情
     * @return 结果
     */
    @Override
    public int updateOrderDetail(OrderDetail orderDetail)
    {
        return orderDetailMapper.updateOrderDetail(orderDetail);
    }

    /**
     * 删除订单详情对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteOrderDetailByIds(String ids)
    {
        return orderDetailMapper.deleteOrderDetailByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除订单详情信息
     * 
     * @param goodsId 订单详情ID
     * @return 结果
     */
    @Override
    public int deleteOrderDetailById(Long goodsId)
    {
        return orderDetailMapper.deleteOrderDetailById(goodsId);
    }

    /*********************************************************************************************************************
     * @Author:  王海新
     * @Date:  20:49  2020/11/23
     * @Version:  1.0.0
     * @Description:  生成订单 和订单详情
     */
    @Override
    public void saveOrder(List<OrderDetail> orderDetails , String name) {
        //生成订单，查看订单的bean，然后将数据封装进去
        //生成订单编号
        String orderCode = UUID.randomUUID().toString();
        //封装订单数量
        int orderCount = 0 ;
        //封装订单总价
        BigDecimal bigDecimal = new BigDecimal(0);
        for (OrderDetail orderDetail : orderDetails){
            orderCount += Math.toIntExact(orderDetail.getGoodsCount());
            bigDecimal = bigDecimal.add(orderDetail.getGoodsTotalMoney());
        }
        Order order = new Order();
        order.setOrderCode(orderCode);
        order.setOrderCount(orderCount);
        order.setOrderTotalMoney(bigDecimal);
        order.setVipName(name);
        orderMapper.insertOrder(order);
        //保存详情
        for (int i = 0; i < orderDetails.size() ; i++) {
            orderDetails.get(i).setOrderCode(orderCode);
            orderDetailMapper.insertOrderDetail(orderDetails.get(i));

        }

    }
}
